<script setup lang="ts">
import {defineAsyncComponent, reactive, ref} from "vue";
import {ElMessage, FormInstance, FormRules, UploadProps} from "element-plus";
const userList = defineAsyncComponent(() => import('./userList.vue'));
const emit = defineEmits(['changeShow'])
const upDataRef = ref()
const userListRef = ref()
const formRef = ref()
const action = import.meta.env.VITE_API_URL + '/util/file/oss/single'
const imageUrl = ref('')
const data = reactive({
  dialogTableVisible: false,
  imageUrl: null,
  form: {
    messageHeadline: '',
    messageContent: '',
  },
})
const rules = reactive<FormRules>({
  messageHeadline: [
    {
      required: true,
      message: 'Please fill in the title',
      trigger: 'blur',
    },
  ],
  messageContent: [
    {
      required: true,
      message: 'Please fill in the contents',
      trigger: 'blur',
    },
  ],
})
const show = () => {
  data.dialogTableVisible = true
}
const editSubmission = () => {
  formRef.value.validate().then((res:any) => {
    if (res){
      userListRef.value.show(data.form)
    }
  })
}
const showFalseFun = () => {
  data.dialogTableVisible = false
  formRef.value.resetFields()
  upDataRef.value.clearFiles()
  data.form = {
    messageHeadline: '',
    messageContent: '',
  }
  data.imageUrl = null
  emit('changeShow')
}
const closeFun = () => {
  data.form.messageHeadline = ''
  data.form.messageContent = ''
}
defineExpose({
  data,
  show
})
</script>

<template>
  <div>
    <el-dialog v-model="data.dialogTableVisible" :align-center="true" title="Send a message" @close="closeFun">
      <el-form ref="formRef" style="width: 100%" :model="data.form" class="demo-form-inline" :rules="rules">
        <el-form-item label="Message title" prop="messageHeadline">
          <el-input v-model="data.form.messageHeadline"/>
        </el-form-item>
        <el-form-item label="Content" prop="messageContent">
          <el-input type="textarea" :rows="4" show-word-limit maxlength="150" v-model="data.form.messageContent" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button type="primary" @click="editSubmission">Next</el-button>
      </template>
    </el-dialog>
    <userList ref="userListRef" @showFalse="showFalseFun"/>
  </div>
</template>

<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>